package com.itheima.service.impl;


import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //添加操作
    @Override
    public void add(ArrayList<OrderSetting> list) {
        //进行数据的非空判断
        if (list != null && list.size() > 0) {
            //进行数据的遍历
            for (OrderSetting orderSetting : list) {
                //根据时间查询是否已经存在数据
                Date date = orderSetting.getOrderDate();
                //根据时间进行查询
                Long count = orderSettingDao.findCountByOrderDate(date);
                //如果count大于0表示的是，此时间已经设定了数据，执行的是修改的操作
                if (count > 0){
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //反之执行的是添加的操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据月份进行获取数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        return orderSettingDao.getOrderSettingByMonth(date);
    }
}
